load(
    "//ign_bazel:build_defs.bzl",
    "IGNITION_ROOT",
    "IGNITION_VISIBILITY",
    "cmake_configure_file",
    "generate_include_header",
    "generate_yaml",
    "ign_config_header",
    "ign_export_header",
)
load(
    ":ign_msg_gen.bzl",
    "get_proto_headers",
    "ign_msg_gen",
)

package(
    default_visibility = IGNITION_VISIBILITY,
    features = [
        "-parse_headers",
        "-layering_check",
    ],
)

licenses(["notice"])

exports_files(["LICENSE"])

PROJECT_NAME = "ignition-msgs"

PROJECT_MAJOR = 8

PROJECT_MINOR = 0

PROJECT_PATCH = 0

# Generates config.hh based on the version numbers in CMake code.
ign_config_header(
    name = "config",
    src = "include/ignition/msgs/config.hh.in",
    cmakelists = ["CMakeLists.txt"],
    project_name = PROJECT_NAME,
    project_version = (PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH),
)

ign_export_header(
    name = "include/ignition/msgs/Export.hh",
    export_base = "IGNITION_MSGS",
    lib_name = "ignition-msgs",
    visibility = ["//visibility:private"],
)

public_headers_no_gen = glob([
    "include/ignition/msgs/*.hh",
    "include/ignition/msgs/detail/*.hh",
])

protos = glob(["proto/ignition/msgs/*.proto"])

generate_include_header(
    name = "messagetypeshh_genrule",
    out = "include/ignition/msgs/MessageTypes.hh",
    hdrs = get_proto_headers(protos),
    strip_prefix = ["ign_msgs"],
)

generate_include_header(
    name = "msghh_genrule",
    out = "include/ignition/msgs.hh",
    hdrs = public_headers_no_gen + [
        "include/ignition/msgs/config.hh",
        "include/ignition/msgs/Export.hh",
        "include/ignition/msgs/MessageTypes.hh",
    ],
)

public_headers = public_headers_no_gen + [
    "include/ignition/msgs/config.hh",
    "include/ignition/msgs/Export.hh",
    "include/ignition/msgs/MessageTypes.hh",
    "include/ignition/msgs.hh",
]

# Custom Ignition Protoc plugin
cc_binary(
    name = "ign_msgs_gen",
    srcs = [
        "src/Generator.cc",
        "src/Generator.hh",
        "src/generator_main.cc",
    ],
    deps = [
        "@com_google_protobuf//:protobuf",
        "@com_google_protobuf//:protoc_lib",
    ],
)

# Create a library of our protobuf message files
proto_library(
    name = "ignmsgs_proto",
    srcs = protos,
    strip_import_prefix = "proto",
)

# Create a library of our protobuf message files
proto_library(
    name = "ignmsgs_proto_public",
    srcs = protos,
    strip_import_prefix = "proto",
)

# Generate our custom CC files from the protos
ign_msg_gen(
    name = "ignmsgs_proto_cc",
    deps = [":ignmsgs_proto"],
)

cc_library(
    name = "ign_msgs",
    srcs = [
        "src/Factory.cc",
        "src/Filesystem.cc",
        "src/Utility.cc",
        ":ignmsgs_proto_cc",
    ],
    hdrs = public_headers,
    includes = ["include"],
    deps = [
        ":ignmsgs_proto_cc",
        IGNITION_ROOT + "ign_math",
        "@com_google_protobuf//:protobuf",
        "@tinyxml2",
    ],
)

# use shared library only when absolutely needd
cc_binary(
    name = "libignition-msgs.so",
    srcs = [
        "src/ign.cc",
        "src/ign.hh",
    ],
    includes = ["include"],
    linkshared = True,
    linkstatic = True,
    deps = [
        ":ign_msgs",
    ],
)

[cc_test(
    name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
    srcs = [src],
    data = [IGNITION_ROOT + "ign_msgs/test:desc/stringmsg.desc"],
    deps = [
        ":ign_msgs",
        IGNITION_ROOT + "ign_math",
        IGNITION_ROOT + "ign_msgs/test:test_utils",
        "@gtest",
        "@gtest//:gtest_main",
    ],
) for src in glob(
    [
        "src/*_TEST.cc",
    ],
)]

cmake_configure_file(
    name = "msgs.rb",
    src = "src/cmd/cmdmsgs.rb.in",
    out = "cmdmsgs.rb",
    cmakelists = ["CMakeLists.txt"],
    defines = [
        "library_location=libignition-msgs.so",
        "PROJECT_VERSION_FULL=%d.%d.%d" % (PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH),  # noqa
        "IGN_LIBRARY_NAME=%s" % [PROJECT_NAME],
    ],
)

CMDS = "    - msg   : Print information about messages."

generate_yaml(
    name = "msgs",
    commands = CMDS,
    library_name = PROJECT_NAME,
    library_version = "%d.%d.%d" % (PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH),
    ruby_target = "msgs.rb",
)
